/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MediumManagement;

import Utilities.Functions;
import Utilities.PermissionsConstants;
import org.virtualbox_3_2.MediumVariant;

/**
 * Instances of this class are used by the user interface when a request to
 * create a new hard disk is sent to the media management service.
 *
 * @author Angel Sanadinov
 */
public class CreateHardDiskRequest extends MediumManagementRequest
{
    private String format;          //hard disk format
    private String location;        //hard disk location on the host
    private long logicalSize;       //size of the disk
    private MediumVariant varinat;  //medium variant
    private String permissions;     //hard disk permissions
    private String parentId;        //disk parent, if available

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public CreateHardDiskRequest()
    {
        this.format = null;
        this.location = null;
        this.logicalSize = 0;
        this.varinat = null;
        this.parentId = null;
    }

    /**
     * Constructs a "create hard disk" request with the specified data. <br><br>
     *
     * For basic data setup.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the hard disk will reside
     * @param format the hard disk format
     * @param location the location of the disk on the host
     * @param variant the medium variant of the disk
     * @param permissions permissions data to be associated with the new disk
     */
    private CreateHardDiskRequest(int requestorId, int serverId, String format, String location, MediumVariant variant, String permissions)
    {
        setRequestorId(requestorId);
        setServerId(serverId);
        this.permissions = permissions;
        this.format = format;
        this.location = location;
        this.varinat = variant;
    }

    /**
     * Constructs a create hard disk request with the specified data. <br><br>
     *
     * Used for creating a normal hard disk.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the hard disk will reside
     * @param format the hard disk format
     * @param location the location of the disk on the host
     * @param variant the medium variant of the disk
     * @param logicalSize the size of the hard disk (in MBs)
     * @param permissions permissions data to be associated with the new disk
     */
    public CreateHardDiskRequest(int requestorId, int serverId, String format, String location, MediumVariant variant, long logicalSize, String permissions)
    {
        this(requestorId, serverId, format, location, variant, permissions);
        this.logicalSize = logicalSize;
        this.parentId = null;
        setRequestedAction(PermissionsConstants.ACTION_MEDIUM_CREATE_HDD);
    }

    /**
     * Constructs a create hard disk request with the specified data. <br><br>
     *
     * Used for creating differencing hard disks.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the hard disk will reside
     * @param format the hard disk format
     * @param location the location of the disk on the host
     * @param variant the medium variant of the disk
     * @param permissions permissions data to be associated with the new disk
     * @param parentId the id of the hard disk's parent
     */
    public CreateHardDiskRequest(int requestorId, int serverId, String format, String location, MediumVariant variant, String permissions, String parentId)
    {
        this(requestorId, serverId, format, location, variant, permissions);
        this.parentId = parentId;
        this.logicalSize = 0;
        setRequestedAction(PermissionsConstants.ACTION_MEDIUM_CREATE_HDD_DIFF);
    }

    /**
     * Retrieves the hard disk format.
     *
     * @return the medium format
     */
    public String getFormat()
    {
        return format;
    }

    /**
     * Retrieves the location of the hard disk on the host.
     *
     * @return the location of the disk
     */
    public String getLocation()
    {
        return location;
    }

    /**
     * Retrieves the medium variant of the disk.
     *
     * @return the medium variant
     */
    public MediumVariant getVarinat()
    {
        return varinat;
    }

    /**
     * Retrieves the permissions of the new hard disk.
     *
     * @return the hard disk's permissions
     */
    public String getPermissions()
    {
        return permissions;
    }

    /**
     * Retrieves the size of the disk.
     *
     * @return the disk size (in MBs)
     */
    public long getLogicalSize()
    {
        return logicalSize;
    }

    /**
     * Retrieves the id of the parent of the new hard disk.
     *
     * @return the parent id
     */
    public String getParentId()
    {
        return parentId;
    }

    /**
     * Sets the hard disk format.
     *
     * @param format the disk format
     */
    public void setFormat(String format)
    {
        this.format = format;
    }

    /**
     * Sets the hard disk's location on the host.
     *
     * @param location the disk location
     */
    public void setLocation(String location)
    {
        this.location = location;
    }

    /**
     * Sets the medium variant of the disk.
     *
     * @param varinat the medium variant
     */
    public void setVarinat(MediumVariant varinat)
    {
        this.varinat = varinat;
    }

    /**
     * Sets the permissions of the disk.
     *
     * @param permissions the disk's permissions
     */
    public void setPermissions(String permissions)
    {
        this.permissions = permissions;
    }

    /**
     * Set the size of the disk.
     *
     * @param logicalSize the disk size (in MBs)
     */
    public void setLogicalSize(long logicalSize)
    {
        this.logicalSize = logicalSize;
    }

    /**
     * Sets the id of the disk's parent.
     *
     * @param parentId the parent id
     */
    public void setParentId(String parentId)
    {
        this.parentId = parentId;
    }

    @Override
    public boolean isValid()
    {
        boolean result = false;

        if(!super.isValid() || format == null || !Functions.isLocationValid(location)
                || varinat == null || !Functions.arePermissionsValid(permissions))
            return result;
        else
            ;
        
        switch(getRequestedAction())
        {
            case PermissionsConstants.ACTION_MEDIUM_CREATE_HDD:
            {
                if(logicalSize > 0)
                    result = true;
                else
                    result = false;
            } break;

            case PermissionsConstants.ACTION_MEDIUM_CREATE_HDD_DIFF:
            {
                if(parentId != null)
                    result = true;
                else
                    result = false;
            } break;

            default: result = false;
        }

        return result;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (mediumId,format,location,logicalSize,variant,
     * permissions,parentId)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return CreateHardDiskRequest.class.getSimpleName()
                + ": (" + getMediumId() + "," + format + "," + location 
                + "," + logicalSize + "," + varinat + "," + permissions
                + "," + parentId + ")";
    }
}
